package com.project.livetv.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.project.livetv.R;
import com.project.livetv.callback.NetCallBack;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by li on 2016/5/9.
 */
public abstract class BaseActivity extends AppCompatActivity {


    private Callback.Cancelable cancelable;

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        initView();
        initData();
        setData();
        setListener();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

    }
    public abstract void initView();

    public abstract void initData();

    public abstract void setData();

    public abstract void setListener();



    //返回主界面
    public  void backFinish(View view){
        finish();
        overridePendingTransition(R.anim.activity_zoom_out,R.anim.activity_zoom_in);
    }


    /**
     * 下载方法
     */
    protected void downLoadDataInActivity(String url, final NetCallBack netCallBack){
        RequestParams params = new RequestParams(url);
        if(cancelable !=null&& cancelable.isCancelled()){
            cancelable.cancel();
        }
        netCallBack.onStart();
        cancelable = x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                netCallBack.onSucess(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                netCallBack.onError(ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {

                netCallBack.onCancled();
            }

            @Override
            public void onFinished() {
                netCallBack.onFinished();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(cancelable!=null&&cancelable.isCancelled()){
            cancelable.cancel();
        }
    }
}
